package com.zh.api.controller.family;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.FamilyDO;
import com.zh.familyInfo.service.FamilyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Api("家庭信息【资料管理控制层】")
public class FamilyController extends BaseController {
	@Autowired
	private FamilyService familyService;

	@Log("显示家庭信息资料")
	@ResponseBody
	@RequestMapping(value = "listFamilyInformation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO listFamilyInformation(@RequestBody Map<String, Object> params){
		if(params==null){
			return ResultVO.success(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
		params.put("number",getFamilyUser().getNumber());
        Query query = new Query(params);
		List<FamilyDO> familyList = familyService.list(query);
		int total = familyService.count(query);
		PageUtils pageUtils = new PageUtils(familyList, total);
		return ResultVO.success(pageUtils);
	}
	@Log("显示当前家庭资料")
	@ResponseBody
	@RequestMapping(value = "editFamilyInformationById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    public 	ResultVO editFamilyInformationById(@RequestBody FamilyDO family){
		if(family==null || CheckEmptyUtil.isEmpty(family.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		FamilyDO familys = familyService.get(family.getId());
	    return ResultVO.success(familys);
	}

	@Log("添加家庭信息资料")
	@ResponseBody
	@RequestMapping(value = "saveFamilyInformation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO saveFamilyInformation(@RequestBody FamilyDO family){
		if(family==null ){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		family.setCreateBy(getUserId());
		if(familyService.save(family)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}


	@Log("更新家庭成员资料")
	@ResponseBody
	@RequestMapping(value = "updateFamilyInformation",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO updateFamilyInformation(@RequestBody  FamilyDO family){
		if(family==null || CheckEmptyUtil.isEmpty(family.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		family.setUpdateBy(getUserId());
		familyService.update(family);
		return ResultVO.success();
	}

	@Log("删除家庭成员资料")
	@ResponseBody
	@RequestMapping(value = "removeFamilyInformationById",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public ResultVO removeFamilyInformationById(@RequestBody FamilyDO family){
		if(family==null || CheckEmptyUtil.isEmpty(family.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(familyService.remove(family.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@RequestMapping(value = "batchRemoveFamilyInformationByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量家庭成员资料")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(familyService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}
}
